package com.istock.user.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.union.user.model.Constants;
import com.istock.union.user.model.OperLog;
import com.istock.union.user.model.SecurityChecker;
import com.istock.union.user.model.TenantInfo;
import com.istock.union.user.service.TenantInfoService;

@Controller
@RequestMapping("/s/tenantInfo")
public class TenantInfoController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private TenantInfoService tenantInfoService;
	
	@RequestMapping("showList")
	@OperLog(functionName="查询" , menuName ="租户管理")
	public String showTenant(ModelMap model) {
		return "user-app/tenant/list";
	}
	
	@RequestMapping("search")
	@SecurityChecker("user:tenantInfo:search")
	public @ResponseBody ApiBaseResponse<PageInfo<TenantInfo>> search(TenantInfo tenantInfo , PageInfo page){
		if(page == null){
			page = new PageInfo();
		}
		tenantInfoService.findTanantList(tenantInfo, page);
		return new ApiBaseResponse<PageInfo<TenantInfo>>(RetStatusEnum.SUCCESS , Constants.API_RESPONSE_SUCCESS_CODE,Constants.API_RESPONSE_SUCCESS_MESSAGE , page);
		//return tenantInfoService.findTanantList(tenantInfo, page);
	}
	
	@RequestMapping("addPre")
	@OperLog(functionName="新增" , menuName ="租户管理")
	@SecurityChecker("user:tenantInfo:add")
	public String addPre(ModelMap model , String id) {
		if(StringUtils.isNotBlank(id)) {
			TenantInfo tenantInfo = tenantInfoService.findTenantInfoById(id);
			model.put("edit", tenantInfo);
		}
		return "user-app/tenant/save";
	}
	
	@RequestMapping("editPre")
	@OperLog(functionName="修改" , menuName ="租户管理")
	@SecurityChecker("user:tenantInfo:edit")
	public String editPre(ModelMap model , String id) {
		return addPre(model , id);
	}
	
	@RequestMapping("save")
	@OperLog(functionName="保存" , menuName ="租户管理")
	public @ResponseBody ApiBaseResponse<Boolean> save(TenantInfo tenantInfo){
		try {
			tenantInfoService.saveTenantInfo(tenantInfo);
			return new ApiBaseResponse<Boolean>(RetStatusEnum.SUCCESS , Constants.API_RESPONSE_SUCCESS_CODE,Constants.API_RESPONSE_SUCCESS_MESSAGE , true);
		}catch(Exception e) {
			logger.error("save exception " , e);
		}
		return new ApiBaseResponse<Boolean>(RetStatusEnum.FAIL , Constants.API_RESPONSE_FAIL_CODE,Constants.API_RESPONSE_FAIL_MESSAGE , false);
	}
}
